package cn.itcast.hotel;


import cn.itcast.hotel.pojo.HotelSDEDoc;
import cn.itcast.hotel.repository.HotelRepository;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SpringBootTest
public class SpringDataEsManager {
    @Autowired
    private HotelRepository hotelRepository;

    @Test // 保存和批量保存
    public void testAddHotelSDEDoc() {
        HotelSDEDoc hotelSDEDoc = new HotelSDEDoc(1L, "顺义如家", 300, "如家", "北京");
        hotelRepository.save(hotelSDEDoc);

        List<HotelSDEDoc> list = new ArrayList<>();
        list.add(new HotelSDEDoc(1L, "顺义如家", 299, "如家", "北京"));
        list.add(new HotelSDEDoc(2L, "海淀汉庭", 299, "汉庭", "北京"));
        list.add(new HotelSDEDoc(3L, "桥头汉庭", 199, "汉庭", "天津"));
        list.add(new HotelSDEDoc(4L, "滨海如家", 199, "如家", "天津"));
        list.add(new HotelSDEDoc(5L, "上海喜来登", 2000, "喜来登", "上海"));
        hotelRepository.saveAll(list);

    }

    @Test //更新，就是用保存方法
    public void testUpdateHotelSDEDoc() {
        HotelSDEDoc hotelSDEDoc = new HotelSDEDoc(1L, "顺义如家22222", 300, "如家", "北京");
        hotelRepository.save(hotelSDEDoc);
    }

    @Test //根据id删除，删除所有
    public void testDeleteHotelSDEDoc() {
        hotelRepository.deleteById(1L);
        hotelRepository.deleteAll();
    }

    @Test //根据id查询 查询所有  分页查询
    public void testQueryHotelSDEDoc() {
        Optional<HotelSDEDoc> hotelSDEDocOptional = hotelRepository.findById(1L);
        HotelSDEDoc hotelSDEDoc = hotelSDEDocOptional.get();
        System.out.println(hotelSDEDoc);

        System.out.println("==================================");
        Iterable<HotelSDEDoc> hotelSDEDocIterable = hotelRepository.findAll();
        for (HotelSDEDoc hotelSDEDoc1 : hotelSDEDocIterable) {
            System.out.println(hotelSDEDoc1);
        }
        System.out.println("------------------------------------");
        Pageable pageable = PageRequest.of(0, 3, Sort.by(Sort.Direction.DESC, "price"));
        Page<HotelSDEDoc> hotelSDEDocPage = hotelRepository.findAll(pageable);
        List<HotelSDEDoc> content = hotelSDEDocPage.getContent();
        for (HotelSDEDoc hotelSDEDoc1 : content) {
            System.out.println(hotelSDEDoc1);
        }


    }
    @Test
    public void testFindCondition(){
//      List<HotelSDEDoc> hotelList =  hotelRepository.findByName("顺义");
//      List<HotelSDEDoc> hotelList =  hotelRepository.findByBrand("如家");
//      List<HotelSDEDoc> hotelList =  hotelRepository.findByPriceBetween(200.0,300.0);
        List<HotelSDEDoc> hotelList =  hotelRepository.findByNameAndBrand("顺义","如家");
        System.out.println(hotelList);
    }
    @Test
    public void testRepositoryAndRestClient(){
        QueryBuilder query = QueryBuilders.matchQuery("name","如家");
        Pageable pageable = PageRequest.of(0,3,Sort.by(Sort.Direction.DESC,"price"));
        Iterable<HotelSDEDoc> hotelIterable = hotelRepository.search(query,pageable);
        for (HotelSDEDoc hotel : hotelIterable) {
            System.out.println(hotel);
        }
    }

}
